package org.test.client.ui.component.label;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIUtil;

public class MyIconLabel extends JLabel{
	private String path = null;
	private String info = null;
	private Font font = null;
	private int h = 0;
	
	private BufferedImage img = null;
	private int actualW = 0;
	private  int stringWidth = 0;
	private  int stringAscent = 0;
	
	public MyIconLabel(String path,String info,Font font,int h){
		this.path = path;
		this.info = info;
		this.font = font;
		this.h = h;
		
		img = Icons.loadImg(path);
		int imgW = img.getWidth();
		int imgH = img.getHeight();
		actualW = (int)((double)imgW * ((double)h/(double)imgH));
		
		FontMetrics fm = this.getFontMetrics(font);
		stringWidth = fm.stringWidth(info);
		stringAscent = fm.getAscent();
		
		this.setIcon(new ImageIcon(img));
		this.setText(info);
		this.setPreferredSize(new Dimension(actualW+stringWidth, h));
		this.setOpaque(false);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
//		super.paintComponent(g);
		UIUtil.setAntiAliasing((Graphics2D)g, true);
		g.drawImage(img,0, 0, actualW, h, null);
		g.setFont(font);
		g.setColor(Color.WHITE);
		g.drawString(info, actualW, h/2+stringAscent/2-2);
	}
	
}
